package com.rhb.af.web.struts.tag;


import java.text.*;

import org.apache.struts.taglib.*;
import javax.servlet.jsp.JspException;

/**
 * TextTag will be declared in struts-html.tld, as follow
 * <name>text</name>
 * <tagclass>com.rhb.xgerp4.struts.taglib.TextTag</tagclass>
 * <attribute>
 * <name>format</name>
 * <required>false</required>
 * <rtexprvalue>true</rtexprvalue>
 * </attribute>
 * ....
 * 
 * @author caozhuan
 *
 */
public class TextTag extends org.apache.struts.taglib.html.TextTag{
	private static final long serialVersionUID = 1L;
	public TextTag() {
		super();
	}
	

	protected String formatStr = null;
	public String getFormat() {
		return (this.formatStr);
	}
	public void setFormat(String formatStr) {
		this.formatStr = formatStr;
	}

	protected String formatValue(Object value) throws JspException {
		if (value == null) {
			return "";
		}
		
		Format format = null;
		String outstr;
		if (value instanceof java.lang.String) {
			outstr = (String)value;
		}else if(formatStr==null){
			outstr = value.toString();
		}else if(value instanceof Number){
			format = new DecimalFormat(formatStr);
			outstr = format.format(value);
		}else if (value instanceof java.util.Date){
			format = new SimpleDateFormat(formatStr);
			outstr = format.format(value);
		}else outstr = value.toString();
		
		return TagUtils.getInstance().filter(outstr);
	}
	
	public void release(){
		super.release();
		formatStr = null;
	}
}